package com.haier.uos.springcloud.basic.service.impl;

import com.haier.uos.springcloud.basic.service.ICityDataService;
import com.haier.uos.springcloud.basic.util.XmlBuilder;
import com.haier.uos.springcloud.basic.vo.City;
import com.haier.uos.springcloud.basic.vo.CityList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import javax.xml.bind.JAXBException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

@Service
public class CityDataServiceImpl implements ICityDataService {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public List<City> getCityList() {
        // 读取配置文件
        Resource resource = new ClassPathResource("citylist.xml");
        StringBuffer sb = new StringBuffer();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), "utf-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            logger.error("获取城市列表xml失败", e);
        }

        CityList cityList = new CityList();
        try {
            cityList = (CityList) XmlBuilder.xmlStrToObject(CityList.class, sb.toString());
        } catch (JAXBException e) {
            logger.error("解析城市列表xml失败", e);
        }

        return cityList.getCities();
    }
}
